﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ASPMVCUtil.Helpers.Html.Grid
{
    public class Key
    {
        const char KeyStringSeparartor = '_';
        public List<KeyMember> Members { get; set; }
        public string KeyString { 
            get 
            {
                string keyString = "";
                foreach (KeyMember keyMember in Members)
                {
                    if(keyString.Equals(""))
                        keyString += keyMember.Value;
                    else
                        keyString += KeyStringSeparartor.ToString() + keyMember.Value;
                }
                return keyString;
            } 
        }

        public Key(List<KeyMember> Members)
        {
            this.Members = Members;
        }

        //From selected id
        public Key(string id, string[] keyMembersNames)
        {
            List<KeyMember> members = new List<KeyMember>();
            string[] keyValues = id.Split(new char[] { KeyStringSeparartor }, StringSplitOptions.RemoveEmptyEntries);
            if(keyValues.Length != keyMembersNames.Length)
                throw new ArgumentException(GridResources.InvalidInput4);
            for(int i = 0; i < keyValues.Length; i++) 
            {
                members.Add(new KeyMember() { PropertyName = keyMembersNames[i], Value = keyValues[i] });
            }
            this.Members = members;
        }
    }
}
